layui.use(['table', 'admin', 'ax', 'func','form'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var func = layui.func;
    var form = layui.form;

    /**
     * 管理
     */
    var Archives = {
        tableId: "archivesTable"
    };

    /**
     * 检索店名
     */
    var languageAjax = new $ax(Feng.ctxPath + "/btyWage/getDm");
    var languageResult = languageAjax.start();
    $.each(languageResult, function( index, val ) {
        $("#dm").append("<option value='"+val.dm+"'>"+val.dm+"</option>");
    } );
    form.render('select');

    /**
     * 初始化表格的列
     */
    Archives.initColumn = function () {
        return [[
            {type: 'checkbox', fixed: true},
            {field: 'xm', align: "left", title: '姓名',width:80, fixed: true},
            {field: 'id', align: "left", hide: true, sort: true, title: 'id'},
            {field: 'bh', align: "left", title: '编号',minWidth:165},
            {field: 'dm', align: "left", title: '店名',minWidth:160},
            {field: 'zw', align: "left", title: '职务',width: 60},
            {field: 'rq', align: "left", title: '入职日期',width:120},
            {field: 'tel', align: "left", title: '电话',width: 120},
            {field: 'sfz', align: "left", title: '身份证',width: 178},
            {field: 'yhk', align: "left", title: '银行卡',width:180},
            {field: 'qy', align: "left", title: '区域',width:90},
            {field: 'eqy', align: "left", title: '二级区域',width:90},
            {field: 'gz', align: "center", title: '工资',width:65},
            {field: 'bz', align: "left", title: '备注',width: 100},
            {align: 'center', toolbar: '#tableBar', title: '操作', width: 165}
        ]];
    };
    // 重置按钮事件
    $('#btnReset').click(function () {
        $('input').val('');
        $("#dm").val('');
        form.render();
    });
    /**
     * 点击查询按钮
     */
    Archives.search = function () {
        var queryData = {};
        queryData['xm'] = $("#xm").val();
        queryData['dm'] = $("#dm").val();
        queryData['tel'] = $("#tel").val();
        queryData['sfz'] = $("#sfz").val();
        table.reload(Archives.tableId, {
            where: queryData, page: {curr: 1}
        });
    };


    /**
     * 弹出添加对话框
     */
    Archives.openAddDlg = function () {
        func.open({
            height: 650,
            title: '添加',
            content: Feng.ctxPath + '/archives/add',
            tableId: Archives.tableId
        });
    };

     /**
      * 点击编辑
      *
      * @param data 点击按钮时候的行数据
      */
      Archives.openEditDlg = function (data) {
          func.open({
              title: '修改',
              content: Feng.ctxPath + '/archives/edit/' + data.id,
              tableId: Archives.tableId
          });
      };


    /**
     * 导出excel按钮
     */
    Archives.exportExcel = function () {
        var checkRows = table.checkStatus(Archives.tableId);
        if (checkRows.data.length === 0) {
            // Feng.error("请选择要导出的数据");

            //获取导出Excel的数据
            var ajax = new $ax(Feng.ctxPath + "/archives/listExport");
            var result = ajax.start();
            var lists=result;
            for (index in lists) {
                var yhk = lists[index].yhk;
                lists[index].yhk = yhk + "\t";
            }
            table.exportFile(tableResult.config.id, lists, 'xls');

        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    Archives.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/archives/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(Archives.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    /**
     * 同步档案
     *
     * @param data 点击按钮时候的行数据
     */
    Archives.onUpdateItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/archives/recovery", function (data) {
                Feng.success("同步成功!");
                table.reload(Archives.tableId);
            }, function (data) {
                Feng.error("同步成功!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否同步到档案表?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + Archives.tableId,
        url: Feng.ctxPath + '/archives/list',
        page: true,
        limit: 20,
        height: "full-59",
        cellMinWidth: 100,
        cols: Archives.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        Archives.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {

    Archives.openAddDlg();

    });

    // 导出excel
    $('#btnExp').click(function () {
        Archives.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + Archives.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            Archives.openEditDlg(data);
        } else if (layEvent === 'delete') {
            Archives.onDeleteItem(data);
        }else if (layEvent === 'update'){
            Archives.onUpdateItem(data);
        }
    });
});
